//
//  FindCar_Model_TableViewController.m
//  CarEncyclopedia
//
//  Created by lanou3g on 15/7/19.
//  Copyright (c) 2015年 ThighXingClub. All rights reserved.
//

#import "FindCar_Model_TableViewController.h"
#import "FindCar_Model_TableViewCell.h"
#import "ImageDownLoader.h"
#import "FindCar_Model_Price_TableViewCell.h"
#import "FindCar_Model_SixButton_TableViewCell.h"
#import "FindCar_Model_Style_TableViewCell.h"
#import "FindCar_Collection_ViewController.h"
#import "FindCar_CarDetails_TableViewController.h"
#define Mallurl @"http://baojia.qichecdn.com/priceapi3.9.2/services/seriessummary/get?cityid=0&provinceid=0&salestate="
#define Mallurl1 @"&seriesid="
@interface FindCar_Model_TableViewController ()

@property(nonatomic,retain)NSDictionary *resultDic;

@end

@implementation FindCar_Model_TableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    //设置标题栏是透明的

//    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"透明1.png"] forBarMetrics:UIBarMetricsDefault];
//    
//    
//    self.navigationController.navigationBar.translucent=NO;
    
//    self.navigationController.navigationBar.alpha=0;
    
//    self.navigationController.navigationBar.barStyle=UIBarStyleBlackTranslucent;
//    
    
    
    self.view.backgroundColor=[UIColor colorWithRed:255/255.0 green:215/255.0 blue:0/255.0 alpha:1];
    
    
    [self.tableView registerNib:[UINib nibWithNibName:@"FindCar_Model_TableViewCell" bundle:nil] forCellReuseIdentifier:@"cell3"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"FindCar_Model_Price_TableViewCell" bundle:nil] forCellReuseIdentifier:@"cell4"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"FindCar_Model_SixButton_TableViewCell" bundle:nil] forCellReuseIdentifier:@"cell5"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"FindCar_Model_Style_TableViewCell" bundle:nil] forCellReuseIdentifier:@"cell6"];
    
    NSMutableString *UrlStr=[NSMutableString stringWithFormat:Mallurl,nil];
    NSString *seriesID=[NSString stringWithFormat:@"%@",self.seriesid,nil];
    NSString *pvareaID=[NSString stringWithFormat:@"%@",self.pvareaid,nil];
    
    [UrlStr appendFormat:seriesID,nil];
    //    NSLog(@"%@",UrlStr);
    [UrlStr appendFormat:Mallurl1,nil];
    //    NSLog(@"%@",UrlStr);
    [UrlStr appendFormat:pvareaID,nil];
//        NSLog(@"%@",UrlStr);
    
    NSURL *url=[NSURL URLWithString:UrlStr];
    NSLog(@"%@",url);
    
    
    NSURLRequest *request=[NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (data) {
            NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            
            self.resultDic=[dic objectForKey:@"result"];
            
            
            [self.tableView reloadData];
            
        }
        
        
    }];
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//返回区头
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section!=0) {
        NSString *HeaderStr=[[[self.resultDic objectForKey:@"enginelist"] objectAtIndex:section-1] objectForKey:@"name"];
        return HeaderStr;
    }
    return nil;
    
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    
    NSArray *array=[self.resultDic objectForKey:@"enginelist"];
    // Return the number of sections.
    NSLog(@"section=%lu",array.count);
    return array.count + 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    if (section==0) {
        
        return 3;
    }
    NSArray *array=[[[self.resultDic objectForKey:@"enginelist"] objectAtIndex:section-1] objectForKey:@"speclist"];
    NSLog(@"row=%lu",array.count);
    // Return the number of rows in the section.
    return array.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    

    
    if (indexPath.section==0 && indexPath.row ==0) {
         FindCar_Model_TableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell3"];
        cell.FctnameLabel.text=[self.resultDic objectForKey:@"fctname"];
        cell.LevelnameLabel.text=[self.resultDic objectForKey:@"levelname"];
        
        NSString *PriceStr=[NSString stringWithFormat:@"%@",[self.resultDic objectForKey:@"piccount"]];
        NSMutableString *PriceStr1=[NSMutableString stringWithFormat:PriceStr,nil];
        NSString *PriceStr2=@"张图>";
        [PriceStr1 appendFormat:PriceStr2,nil];
        cell.PriccountLabel.text=PriceStr1;
        
    
    ImageDownLoader *ImageDown=[ImageDownLoader new];
    [ImageDown requestImageUrl:[self.resultDic objectForKey:@"logo"] successBlock:^(NSData *Data) {
        cell.ShowCar_TitleView.image=[UIImage imageWithData:Data];
    } errorBlock:^(NSError *Error) {
        
    }];
        
        return cell;
        

    }
    
       
    if (indexPath.section == 0 && indexPath.row == 1) {
          FindCar_Model_Price_TableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell4"];
    cell.CarNameLabel.text=[self.resultDic objectForKey:@"name"];
    
    NSString *TempStr=@"万";
    NSMutableString *TempStr2=[NSMutableString stringWithFormat:@"%@",[self.resultDic objectForKey:@"fctprice"]];
    [TempStr2 appendFormat:TempStr,nil];
    
    cell.CarPriceLabel.text=TempStr2;
        
    [cell.CollectButton addTarget:self action:@selector(receive:) forControlEvents:UIControlEventTouchUpInside];
        
        return cell;
        
    }
  
    if (indexPath.section==0 && indexPath.row==2) {
        
    FindCar_Model_SixButton_TableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell5"];
        [cell.ConfigurationButton addTarget:self action:@selector(PushMineView:) forControlEvents:UIControlEventTouchUpInside];
        
        
        return cell;

    }
      
    
    
    FindCar_Model_Style_TableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell6"];
    
    
    cell.CarStyle_Label.text=[[[[[self.resultDic objectForKey:@"enginelist"] objectAtIndex:indexPath.section - 1] objectForKey:@"speclist"] objectAtIndex:indexPath.row] objectForKey:@"name"];
    
    NSString *Ten_thousand_Str=@"万";
    NSMutableString *GuidedStr=[NSMutableString stringWithFormat:@"%@",[[[[[self.resultDic objectForKey:@"enginelist"] objectAtIndex:indexPath.section - 1] objectForKey:@"speclist"] objectAtIndex:indexPath.row] objectForKey:@"price"]];
    
    [GuidedStr appendFormat:Ten_thousand_Str,nil];
    cell.Guided_Label.text=GuidedStr;
    
    
    
    NSMutableString *LowsetStr=[NSMutableString stringWithFormat:@"%@",[[[[[self.resultDic objectForKey:@"enginelist"] objectAtIndex:indexPath.section - 1] objectForKey:@"speclist"] objectAtIndex:indexPath.row] objectForKey:@"dealerprice"]];
    [LowsetStr appendFormat:Ten_thousand_Str,nil];
    cell.LowestPrice_Label.text=LowsetStr;
    
    
    
    
    
    // Configure the cell...
    

    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0 && indexPath.row == 0) {
        return 200;
    }
    
    if (indexPath.section==0 && indexPath.row ==1) {
        return 65;
        
    }
    
    if (indexPath.section ==0 && indexPath.row == 2) {
        return 91;
        
    }
    
    return 100;
    
    
}

//点击cell触发的方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (indexPath .section==0 &&indexPath.row ==0) {
        
        
        FindCar_Collection_ViewController *FC_VC=[FindCar_Collection_ViewController new];
        [self.navigationController pushViewController:FC_VC animated:YES];
        
        
        
    }
    
    if (indexPath.section !=0) {
        NSLog(@"123");
        FindCar_CarDetails_TableViewController *FCT_VC=[FindCar_CarDetails_TableViewController new];
        
        [self.navigationController pushViewController:FCT_VC animated:YES];
        
       NSArray *EnginelistArray= [self.resultDic objectForKey:@"enginelist"];
        
        NSDictionary * dic=[EnginelistArray objectAtIndex:indexPath.section - 1];

        NSArray *array=[dic objectForKey:@"speclist"];
        
        NSDictionary *dic1=[array objectAtIndex:indexPath.row];
        
        NSString *ID=[dic1 objectForKey:@"id"];
        
//        NSLog(@"🇬🇧ID==%@",ID);
        
        FCT_VC.specidStr=ID;
        
        FCT_VC.Car_name_Str=[self.resultDic objectForKey:@"name"];
        
        FCT_VC.Car_Style_nameStr=[dic1 objectForKey:@"name"];
        
    }
    
}

-(void)receive:(UIButton *)button{
    //没有数据库功能  后期添加
    
    UIAlertView *View=[[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功啦！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [View show];
    
}

-(void)PushMineView:(UIButton *)Button{
    //界面比较复杂  后期处理
    NSLog(@"456");
    
    
    
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
